/*-----------------------------------------------------------------------------
 * stock_common.c - Common (utility) routines used by the stock server and 
 *                  clients.
 * 
 * March 2010, Forhad Ahmed
 *-----------------------------------------------------------------------------
 */

#include "common/include/stock_common.h"

#define STOCK_MAX_THREAD    10

#define STOCK_PRICE_STR_LEN 10

static char stock_price[STOCK_MAX_THREAD][STOCK_PRICE_STR_LEN];

static int
stock_thread_index()
{
    return 0;
}

char *
stock_price_string(int price) 
{
    char *buff = stock_price[stock_thread_index()];
    char *curr = buff;

    memset(buff, 0, STOCK_PRICE_STR_LEN);

    curr += sprintf(curr, "%d", price / 100);
    curr += sprintf(curr, ".");
    curr += sprintf(curr, price % 100 < 10 ? "0%d" : "%d", price % 100);

    return buff;
}

char *
stock_time_string(uint32_t time) 
{
    char *tp, *p = NULL;

    tp = ctime((time_t *) &time);
    for (p = tp; *p != '\n'; p++);
    *p = '\0';

    return tp;
}


uint32_t
stock_gettimeofday()
{
    struct timeval now;

    gettimeofday(&now, NULL);

    return (uint32_t) now.tv_sec;
}


int
stock_verify_header(void *m)
{
    int            index;
    unsigned char *msg;

    msg = (unsigned char *) m;

    for (index = 0 ; index < STOCK_HEADER_LEN; index++) {
        if (msg[index] != 0xFF) {
            return -1;
        }
    }

    return 0;
}


